package cn.edu.sjtu.se.dslab.rhapsody.parse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


/**
 * the XmiBean represent the whole xmi file
 * @author LongShaohang
 *
 */

public class XmiBean {
	
	private List<UmlModel> umlModels;
	private List<ProfileType> profileTypes;
	
	//infoMap中记录了所有出现过的xmiType，把xmiType作为key，然后value是一个以这些元素的名字为key的map
	private Map<String, Map<String, XmiType>> infoMap;
	//以元素ID作为key的map
	private Map<String, XmiType> idMap;
	
	//a Bean with the file in it
	public XmiBean(String xmiRelativePath){
		this();
		new XmiParser(xmiRelativePath, this).parse();  //解析器，解析的内容还是存在XmiBean中
	}
	
	//a null Xmi Bean
	public XmiBean(){
		super();
		umlModels = new ArrayList<UmlModel>();
		profileTypes = new ArrayList<ProfileType>();
		infoMap = new HashMap<String, Map<String, XmiType>>(); 
		idMap= new HashMap<String ,XmiType>();
	}

	public List<UmlModel> getUmlModels() {
		return umlModels;
	}
	
	public void print(){
		for (UmlModel um : umlModels){
			System.out.println( um.getName() + "--" + um.getId());
			List<XmiType> children = um.getChildren();
			for (XmiType t : children){
				//System.out.println("\t" + t.getXmiType() + "--" + t.getLabel() + "--" + t.getName() + "--" + t.getXmiId());
				printXmiType(t, 0);
			}
		}
		for (ProfileType pt : profileTypes){
			System.out.print(pt.getXmiId() + "--");
			for (Entry<String ,String> e: pt.getProperties().entrySet()){
				System.out.print(e.getKey() + ":" + e.getValue());
			}
			System.out.println("\n");
		}
	}
	
	public void printXmiType(XmiType xt, int depth){
		 if (xt instanceof XmiComposite) {
			 for (int i =0 ; i< depth; ++i)
				 System.out.print("\t");
			 System.out.println(xt.getXmiType() + "--" + xt.getLabel() + "--" + xt.getName() + "--" + xt.getXmiId());
			 for (XmiType subType : ((XmiComposite) xt).getChildren()){
				 printXmiType(subType, depth +1);
			 }
		 }else if (xt instanceof XmiUnit){
			 XmiUnit unit = (XmiUnit)xt;
			 for (int i =0 ; i< depth; ++i)
				 System.out.print("\t");
			 System.out.println(unit.getXmiType() + "--" + unit.getLabel() + "--" + unit.getName() + "--" + unit.getXmiId());
		 }
	}
	
	UmlModel getUmlModel(String name){
		for (UmlModel model : umlModels){
			if (model.getName().equals(name))
				return model;
		}
		return null;
	}

	public void setUmlModels(List<UmlModel> umlModels) {
		this.umlModels = umlModels;
	}

	public void setProfileTypes(List<ProfileType> profileTypes) {
		this.profileTypes = profileTypes;
	}

	public List<ProfileType> getProfileTypes() {
		return profileTypes;
	}
	
	public void save(String fileRelativePath){
		new XmiGenerator(fileRelativePath, this).generate();   //生成文件
	}

	public Map<String, Map<String, XmiType>> getInfoMap() {
		return infoMap;
	}

	public void setInfoMap(Map<String, Map<String, XmiType>> infoMap) {
		this.infoMap = infoMap;
	}

	public Map<String, XmiType> getIdMap() {
		return idMap;
	}

	public void setIdMap(Map<String, XmiType> idMap) {
		this.idMap = idMap;
	}
	
	

}
